/* $begin show-bytes */
#include <stdio.h>
/* $end show-bytes */
#include <stdlib.h>
#include <string.h>
#include <iostream>

/* $begin show-bytes */

using namespace std;

typedef unsigned char *byte_pointer;
//unsigned char * 显示一位

void show_bytes(byte_pointer start, size_t len) {
    size_t i;
    //int k=0;
    for (i = 0; i < len; i++)
	     printf(" %.2x", start[i]);    //line:data:show_bytes_printf
       //buf[k++]=start[i];
    printf("\n");
}


void show_int(int x) {
    show_bytes((byte_pointer) &x, sizeof(int)); //line:data:show_bytes_amp1
}

void show_float(float x) {
    show_bytes((byte_pointer) &x, sizeof(float)); //line:data:show_bytes_amp2
}

void show_pointer(void *x) {
    show_bytes((byte_pointer) &x, sizeof(void *)); //line:data:show_bytes_amp3
}

void show_short(short x){
    show_bytes((byte_pointer) &x, sizeof(short));
}

void show_long(long x){
    show_bytes((byte_pointer) &x, sizeof(long));
}

void show_double(double x){
    show_bytes((byte_pointer) &x, sizeof(double));
}

/* $end show-bytes */


/* $begin test-show-bytes */
void test_show_bytes(int val) {
    int ival = val;
    float fval = (float) ival;
    int *pval = &ival;
    show_int(ival);
    show_float(fval);
    show_pointer(pval);
}
/* $end test-show-bytes */

void simple_show_a() {
/* $begin simple-show-a */
int val = 0x87654321;
byte_pointer valp = (byte_pointer) &val;
show_bytes(valp, 1); /* A. */
show_bytes(valp, 2); /* B. */
show_bytes(valp, 3); /* C. */
show_bytes(valp, 4);
/* $end simple-show-a */
}

void simple_show_b() {
/* $begin simple-show-b */
int val = 0x12345678;
byte_pointer valp = (byte_pointer) &val;
show_bytes(valp, 1); /* A. */
show_bytes(valp, 2); /* B. */
show_bytes(valp, 3); /* C. */
/* $end simple-show-b */
}

void float_eg() {
  int x = 3490593;
  float f = (float) x;
  printf("For x = %d\n", x);
  show_int(x);
  show_float(f);

  x = 3510593;
  f = (float) x;
  printf("For x = %d\n", x);
  show_int(x);
  show_float(f);

}

void string_ueg() {
/* $begin show-ustring */
const char *s = "ABCDEF";
show_bytes((byte_pointer) s, strlen(s));
/* $end show-ustring */
}

void string_leg() {
/* $begin show-lstring */
const char *s = "abcdef";
show_bytes((byte_pointer) s, strlen(s));
/* $end show-lstring */
}

void show_twocomp()
{
/* $begin show-twocomp */
    short x = 12345;
    short mx = -x;

    show_bytes((byte_pointer) &x, sizeof(short));
    show_bytes((byte_pointer) &mx, sizeof(short));
/* $end show-twocomp */
}

//check is_litter_endian
bool is_litter_endian()
{
    int a = 0x123456;
    if( *((char*)&a) == 0x56)
        return 1;
    else
        return 0;
}

int main(int argc, char *argv[])
{
    int val = 12345;


    if (argc > 1) {
	       if (argc > 1) {
	          val = strtol(argv[1], NULL, 0);
	       }
	       printf("calling test_show_bytes\n");
	       test_show_bytes(val);
    }
    else {
	           printf("calling show_twocomp\n");
	           show_twocomp();
	           printf("Calling simple_show_a\n");
	           simple_show_a();
	           printf("Calling simple_show_b\n");
	           simple_show_b();
	           printf("Calling float_eg\n");
	           float_eg();
	           printf("Calling string_ueg\n");
	           string_ueg();
	           printf("Calling string_leg\n");
	           string_leg();


             //exercise 2.59
             if(is_litter_endian())
                cout<<"litter endian"<<endl;
             else
                cout<<"big endian"<<endl;

    }
    return 0;
}
